<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class OrderLogistics extends BaseModel
{
    //订单物流详情
    use HasFactory;
    protected $table = 'order_logistics';
    // 允许批量操作的字段
    protected $fillable = [
        'order_id',
        'finish_product_id',
        'logistics_status',
        'logistics_info',
        'delivery_time',
        'num',
        'sent_num',
        'to_be_sent_num',
        'remark',
        'deliveryman',
        'image1',
        'image2',
        'received_money',
        'created_at','updated_at'];

    public function scopeSearch($query,$params)
    {

        $logistics_status = $params['logistics_status']??3;
        $query->where('logistics_status',$logistics_status);
        $role_id = $params['role_id'];
        if($role_id == 1){
            if(!empty($params['deliveryman_user_id'])){
                $query->where('deliveryman',$params['deliveryman_user_id']);
            }
        }else{
            $query->where('deliveryman',$params['user_id']);
        }
        $query->where('status',1);
        return $query;
    }

    public function order(){
        return $this->hasOne(Order::class,'id','order_id');
    }
    public function image1(){
        return $this->hasOne(File::class,'id','image1');
    }
    public function image2(){
        return $this->hasOne(File::class,'id','image2');
    }
}
